package org.yds.quality.model;

import com.googlecode.aviator.AviatorEvaluator;
import org.yds.quality.dto.GlassDftDTO;
import org.yds.quality.function.dft.SumIf;
import org.yds.quality.utils.FunctionUtils;

import java.util.*;

public class F1 {
    public static void main(String[] args) {
        // 自定义事件监听器（由对方实现）
        EventListener listener = new EventListener() {
            @Override
            public void onEvent(String eventType, Map<String, Object> eventParams) {
                if ("alarm".equals(eventType)) {
                    System.out.println("处理事件 A: " + eventParams);
                }
            }
        };

        // 注册监听器
        EventTrigger.addEventListener(listener);
        AviatorEvaluator.addFunction(new SumIf());
        // 创建 RuleModel 实例
        RuleModel ruleModel = new RuleModel();
        ruleModel.setFunName("SumIf");
        ruleModel.setExpression("SumIf(glassList, 'glass.defectArea > 20 && glass.defectArea < 60') >= 1");
        ruleModel.setEventType("alarm");
        ruleModel.setEventParams(FunctionUtils.getEventParams(Arrays.asList("SumIf")));

        // 创建玻璃列表
        List<GlassDftDTO> glassList = new ArrayList<>();
        glassList.add(new GlassDftDTO("1", 25));
        glassList.add(new GlassDftDTO("2", 70));
        glassList.add(new GlassDftDTO("3", 15));
        glassList.add(new GlassDftDTO("4", 60));
        glassList.add(new GlassDftDTO("5", 30));
        glassList.add(new GlassDftDTO("6", 30));

        // 创建执行上下文
        ExecutionContext executionContext = new ExecutionContext();
        Map<String, Object> env = new HashMap<>();
        env.put("glassList", glassList);
        // 将 env 数据传递到 executionContext
        executionContext.addEnvData(env);

        // 创建规则引擎并执行规则
        RuleEngine ruleEngine = new RuleEngine();
        ruleEngine.executeRule(ruleModel, executionContext);
    }
}
